{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "\n",
    "def get_rich_link(url: str) -> str:\n",
    "  # Send an HTTP GET request to the URL\n",
    "  response = requests.get(\n",
    "    f\"http://iframely.server.crestify.com/iframely?url={url}\"\n",
    "  )\n",
    "\n",
    "  if response.status_code != 200:\n",
    "    # raise HTTP exception\n",
    "    response.raise_for_status()\n",
    "\n",
    "  data = response.json()  # Parse JSON response\n",
    "\n",
    "  # Find the first image link\n",
    "  image_link = \"\"\n",
    "  for link in data[\"links\"]:\n",
    "    if link[\"type\"].startswith(\"image/\"):\n",
    "      image_link = link.get(\"href\", \"\")\n",
    "      break\n",
    "\n",
    "  # Replace excess whitespace and format title and description\n",
    "  title = (\n",
    "    (data[\"meta\"].get(\"title\", \"\"))\n",
    "    .replace(\"\\xa0\", \" \")\n",
    "    .replace(\" \" * 3, \" \")\n",
    "    .strip()\n",
    "  )\n",
    "  description = (\n",
    "    (data[\"meta\"].get(\"description\", \"\"))\n",
    "    .replace(\"\\xa0\", \" \")\n",
    "    .replace(\" \" * 3, \" \")\n",
    "    .strip()\n",
    "  )\n",
    "\n",
    "  # Create the rich link card HTML\n",
    "  rich_link_card_html = f\"\"\"\n",
    "<div class=\"rich-link-card-container\"><a class=\"rich-link-card\" href=\"{url}\" target=\"_blank\">\n",
    "    <div class=\"rich-link-image-container\">\n",
    "        <div class=\"rich-link-image\" style=\"background-image: url('{image_link}')\">\n",
    "        </div>\n",
    "    </div>\n",
    "    <div class=\"rich-link-card-text\">\n",
    "        <h1 class=\"rich-link-card-title\">{title}</h1>\n",
    "        <p class=\"rich-link-card-description\">\n",
    "        {description}\n",
    "        </p>\n",
    "        <p class=\"rich-link-href\">\n",
    "        {url}\n",
    "        </p>\n",
    "    </div>\n",
    "</a></div>\n",
    "\"\"\"\n",
    "  return rich_link_card_html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "<div class=\"rich-link-card-container\"><a class=\"rich-link-card\" href=\"https://www.youtube.com/watch?v=h9WKsqDKj90\" target=\"_blank\">\n",
      "    <div class=\"rich-link-image-container\">\n",
      "        <div class=\"rich-link-image\" style=\"background-image: url('https://i.ytimg.com/vi/h9WKsqDKj90/maxresdefault.jpg')\">\n",
      "        </div>\n",
      "    </div>\n",
      "    <div class=\"rich-link-card-text\">\n",
      "        <h1 class=\"rich-link-card-title\">Mograph and Lighting stream in Blender</h1>\n",
      "        <p class=\"rich-link-card-description\">\n",
      "        Project files here (free, no login): https://www.patreon.com/posts/106925412Discord server: https://discord.gg/polyfjordPatreon: https://patreon.com/polyfjor...\n",
      "        </p>\n",
      "        <p class=\"rich-link-href\">\n",
      "        https://www.youtube.com/watch?v=h9WKsqDKj90\n",
      "        </p>\n",
      "    </div>\n",
      "</a></div>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "rich_link = get_rich_link(\n",
    "  \"https://www.youtube.com/watch?v=h9WKsqDKj90\"\n",
    ")\n",
    "print(rich_link)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
